package com.bjpowernode.controller;

import com.bjpowernode.exception.ExceptionResult;
import com.bjpowernode.exception.GlobalException;
import com.bjpowernode.service.OrderService;
import com.bjpowernode.service.ProviderService;
import com.bjpowernode.service.UserService;
import com.bjpowernode.user.Order;
import com.bjpowernode.user.Provide;
import com.bjpowernode.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.regex.Pattern;

@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private UserService userService;

    @Autowired
    private ProviderService providerService;

    @RequestMapping("/addOrder.do")
    public ExceptionResult addOrder(String name, Integer foodId, String userAddress, String username, String phone) {
        String pattern = "^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$";
        if (!Pattern.matches(pattern,phone))
            throw new GlobalException("请输入正确的手机号");
        Provide provide = providerService.loadProvideFood(foodId);
        if (provide == null)
            throw new GlobalException("此餐品已被下架，请选择其他餐品");
        if (provide.getFoodCount() == 0)
            throw new GlobalException("餐品已被售空，请选择其他餐品");
        Order order = new Order();
        order.setUserId(userService.loadUser(name).getId());
        order.setFoodId(foodId);
        order.setUserAddress(userAddress);
        order.setCreateTime(new Date());
        order.setUserName(username);
        order.setUserPhone(phone);
        orderService.insertOrder(order);
        providerService.updateCount(foodId);
        return ExceptionResult.success();
    }

    @RequestMapping("/takeOrder.do")
    public ExceptionResult takeOrder(String username,Integer orderId) {
        orderService.takeOrder(username, orderId);
        return ExceptionResult.success();
    }

    @RequestMapping("/modifyOrder.do")
    public void modifyOrder(@RequestParam("id") Integer orderId){
        orderService.modifyOrder(orderId);
    }

    @RequestMapping("/cancelOrder.do")
    public ExceptionResult cancelOrder(Integer orderId){
        orderService.cancelOrder(orderId);
        return ExceptionResult.success();
    }

    @RequestMapping("/loadProviderOrder.do")
    public ModelAndView loadProviderOrder(@RequestParam("name") String username){
        ModelAndView mv = new ModelAndView();
        User user = userService.loadUser(username);
        mv.addObject("provideOrder",orderService.findProviderOrder(user.getId()));
        mv.setViewName("provideOrder");
        return mv;
    }

    @RequestMapping("/loadNoTakeOrder.do")
    public ModelAndView loadNoTakeOrder(){
        ModelAndView mv = new ModelAndView();
        mv.addObject("order",orderService.loadNoTakeOrder());
        mv.setViewName("takeOrder");
        return mv;
    }

    @RequestMapping("/loadUserOrder.do")
    public ModelAndView loadUserOrder(String name){
        ModelAndView mv = new ModelAndView();
        mv.addObject("userOrder",orderService.loadUserOrder(name));
        mv.setViewName("order");
        return mv;
    }

    @RequestMapping("/loadUserTakeOrder.do")
    public ModelAndView loadUserTakeOrder(@RequestParam("name") String username){
        ModelAndView mv = new ModelAndView();
        mv.addObject("takeOrder",orderService.loadUserTakeOrder(username));
        mv.setViewName("orderOver");
        return mv;
    }
}
